{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7341b3a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import pymysql\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc0da9d2",
   "metadata": {},
   "source": [
    "### 数据导入数据库，并读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f65ba95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  user_id                   timestamp      group landing_page converted  \\\n",
       "0  851104  2017-01-21 22:11:48.556739    control     old_page         0   \n",
       "1  804228  2017-01-12 08:01:45.159739    control     old_page         0   \n",
       "2  661590  2017-01-11 16:55:06.154213  treatment     new_page         0   \n",
       "3  853541  2017-01-08 18:28:03.143765  treatment     new_page         0   \n",
       "4  864975  2017-01-21 01:52:26.210827    control     old_page         1   \n",
       "\n",
       "         date  \n",
       "0  2017-01-21  \n",
       "1  2017-01-12  \n",
       "2  2017-01-11  \n",
       "3  2017-01-08  \n",
       "4  2017-01-21  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取SQL数据\n",
    "con=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='123456',db='ABTest',use_unicode=True, charset=\"utf8\")\n",
    "data=pd.read_sql(\"select * from ab_data\",con=con)\n",
    "data[\"date\"] = data.timestamp.str[:10]\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f446c6d3",
   "metadata": {},
   "source": [
    "### 一类指标假设检验"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a399f89f",
   "metadata": {},
   "source": [
    "1. 广告做的好不好以点击或下单作为广告的效果评估\n",
    "2. 做广告的同时要注重用户体验(留存，活跃时长，评价，pv等) \n",
    "3. 活动类广告的特点是时效性强，所以给我们的测试时间很短"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d3ab7a3",
   "metadata": {},
   "source": [
    "一类指标:用户活跃时长（保证该指标不被影响）\n",
    "二类指标:广告点击率"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97e2cc6",
   "metadata": {},
   "source": [
    "### 确定检验统计量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6c6ca24",
   "metadata": {},
   "source": [
    "一类指标：用户活跃时长差作为统计量 \n",
    "二类指标：广告点击率 = 点击用户数 / 收到广告用户总数 用不同广告点击率差作为统计量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67ae9a7d",
   "metadata": {},
   "source": [
    "### 埋点数据统计内容"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVIAAACRCAIAAAAevKcWAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAABUqADAAQAAAABAAAAkQAAAADe+6sCAAAxvElEQVR4Ae3decBt1fgHcJFZmYeIUKJIokLdBlMh0vArlShdcnUVhZRbuqVumQtdUVLSwKW6lTEzoUQypkwRQtRtMNPvo+e2zn7XPu/7nt5zzn7PsM4f77v22muvvdez1jOsZz3ftW53u/IrFCgUGDMKrKC9N99885i1uonmrrDCCoWwTRC6vOM2UsDIvP1tfKQULxSYCQV+/etfp8euvPLKL33pS+kyEr/97W+znDPOOOPMM8/MMtPlT3/6029/+9vpsm3ivPPO+9e//tX21phnFrYf8wHQRPNvuummddddt8rq8+bN+8IXvlB994477rjnnnv+/e9/T5kXX3zxsmXL0mWWOOeccz7ykY9kmdnlpz/96Sc/+cn/+Mc/svxyuWIhQaFAvylw6qmnPuc5z1ljjTV+85vf0NJ3vvOd3/CGNxx66KF3utOd8OR666133/ve9/zzz3/FK16B+fFzfM+KK654v/vdr/pt11xzzXXXXXfHO95R5o033njPe96T4SBNpa+88soPeMADpF/4whdeddVVd73rXeNBEmerrbaK8o9+9KNPPvnkyB/zv4Xtx3wA9L35ePL973//xz72sde+9rWU+TrrrPPjH//43HPPPfjgg+l/bL/aaqth+7vd7W6nnHLKH//4xyk+6MMf/rAyj3rUo5T5/ve/f/e73/2yyy6T/tnPfrbddtsddNBB0v/5z39OPPFEL91ll10+97nPkSwSH/jAB373u9+9973vnaLysbpV2H6sunsWGovZfvnLX77+9a//0Y9+dOmll9LVDG/z9nvf+97s/EWLFvmmL37xi+b2//znP+nqN77xjf/+979lfvnLX7788su/9rWv8YyaI+y66650+Mte9rL58+e7u8022+x0y0/6uOOOo9Ul/J773OcyKFTyjne8421vexvL4sUvfvEmm2xy+OGHv+hFL4oy5W+Z25cx0F8K7Lbbbl//+tfvcIc74HB/X/e61z3xiU9cf/316WFq+bTTTvN6KhrPH3nkkX/961/x7ba3/NZcc82nPOUpkfZI9SuV/8pXvuJxiWq+9B577PGpT32KfUHVn3XWWcyKe9zjHhImCCYaWeGxvSzafmy7vqGG3+c+97niiiuuvfba7bff/ic/+cnSpUu9+Prrr2eZv/SlL6WWn/3sZz/rWc+SyRSnz+fMmRNfpuRaa6311Kc+tf6h3/rWtx7+8IdbiHrNa17znve8JytwyCGH0O10PqegaQUlv8oqq+y1116MCP6CrPB4XhZtP5793lyred2e/vSnX3311Wuvvfbxxx//i1/8giB49atfzXR/1atedckll7iMrzHPD3fd1B9n3c5MnkeQf+6jH/0oCz+Vv+CCC1gHT3jCEy688MJ99tmHdPje97633377mepbJrj//e//9re/PRUe50QRfuPc+020/WEPexg1jhU50kPZWpbbYIMNGPk8eXe5y13SR7DzFWCfc+mnzCxBalgF4B3ceuut3frQhz509NFHsyOimJk864Dnj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qeM8ryS+pgVwLEkRzcXvH2Ves2kG4DfZ0j7tlh6QwVqk/IA0LQqZLMmk8RmKDDQb0EFv0HD2wsHMA2zLG/ZnH9e4IC90wJLbzFv3rx5vpkU5+2Dz72lBTcrmYC3bhEHAaEvePugT/lbKIACy4VR0GLQ8PbmYFx6qZ8Y7TY2j8vYVDfSJEIqY7sVmzqlyykSBW8/BXHKrdGmALYvCLx+GWJsS6OnX7WXegsFZkqBNEGeaQXluUKBQoEhpMD/A1ZEMOxTBOhHAAAAAElFTkSuQmCC"
    }
   },
   "cell_type": "markdown",
   "id": "2415d913",
   "metadata": {},
   "source": [
    "\t统计内容\t\t\n",
    "事件\tbanner曝光\tbanner点击\t用户活跃时间\n",
    "事件ID\tbannerid\t\t\n",
    "事件页面\tbannerpage\t\t\n",
    "事件曝光\tbannerex\t\t\n",
    "事件点击\tbannerclick\t\t\n",
    "用户活跃时长\tuseractime\t\t\n",
    "用户活跃开始时间\tuseracbegin\t\t\n",
    "用户活跃结束时间\tuseracend\t\t![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c6e9d79",
   "metadata": {},
   "source": [
    "### 确定原假设和备择假设\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1eaf960",
   "metadata": {},
   "source": [
    "#### 原假设H0 ,备择假设H1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c645ffb7",
   "metadata": {},
   "source": [
    "一类指标：\n",
    "H0:实验组客户活跃时长不显著低于对照组\n",
    "H1:实验组客户活跃时长显著低于对照组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f68c81ca",
   "metadata": {},
   "source": [
    "二类指标：H0:实验组客户广告点击率显著低于对照组 H1:实验组客户广告点击率显著大于对照组\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ad3c67",
   "metadata": {},
   "source": [
    "### 确定显著性水平"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5903894",
   "metadata": {},
   "source": [
    "一类 alpha = 0.05\n",
    "二类 beta = 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "28ba6884",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.05\n",
    "beta = 0.2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "470882c9",
   "metadata": {},
   "source": [
    "### 计算样本量# 参考导老师答案"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b58ce409",
   "metadata": {},
   "source": [
    "根据公式带入 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "74b74879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算treatment_p\n",
    "# 计算对照组广告的点击率（由于converted值为0，1 所以求占比和均值结果一致）\n",
    "control_p = data.converted[(data.group==\"control\") & (data.landing_page==\"old_page\")].astype(float).mean()\n",
    "control_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f50d5410",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10589344218918344"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算p0*(1-p0) = \n",
    "control_p_1_control_p = control_p * (1-control_p)\n",
    "control_p_1_control_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d890bc1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参考导老师答案\n",
    "piA = data.converted[(data.group==\"control\")& (data.landing_page==\"old_page\")].astype(float).mean()\n",
    "piA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a8a13ea6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.13538630450046119"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "piB = piA + 0.015\n",
    "piB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd9bf6ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.959963984540054 0.8416212335729143\n"
     ]
    }
   ],
   "source": [
    "z_alpha = stats.norm.ppf(1-alpha/2)\n",
    "z_beta = stats.norm.ppf(1-beta)\n",
    "print(z_alpha,z_beta)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "819e3e22",
   "metadata": {},
   "source": [
    "#### 计算z1-α ，带入公式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b6271735",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6448536269514729"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z1_alpha = stats.norm.isf(alpha,loc=0,scale=1)\n",
    "Z1_alpha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ef46ecb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8416212335729142"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算z 1-beta\n",
    "# 二类错误的概率\n",
    "Z1_beta = stats.norm.isf(beta,loc=0,scale=1)\n",
    "Z1_beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f821b6a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.13538630450046119, 0.014999999999999986)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "treatment_p = (control_p + 0.015).astype(float)\n",
    "treatment_p\n",
    "p_p0 = (treatment_p - control_p).astype(float)\n",
    "treatment_p,p_p0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5ec50c2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11705685305416959"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_1_p = treatment_p * (1-treatment_p)\n",
    "p_1_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "94f170c3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6126.235378834391"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本量\n",
    "n = (control_p_1_control_p + p_1_p) * ((Z1_alpha +Z1_beta) / p_p0 )**2\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3f465538",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>date</th>\n",
       "      <th>user_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>2859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>6590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>6427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-06</td>\n",
       "      <td>6606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-07</td>\n",
       "      <td>6604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-08</td>\n",
       "      <td>6687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-09</td>\n",
       "      <td>6628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-10</td>\n",
       "      <td>6654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-11</td>\n",
       "      <td>6688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-12</td>\n",
       "      <td>6522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>6552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-14</td>\n",
       "      <td>6548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-15</td>\n",
       "      <td>6714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>6591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>6617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-18</td>\n",
       "      <td>6482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-19</td>\n",
       "      <td>6578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>6534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-21</td>\n",
       "      <td>6749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-22</td>\n",
       "      <td>6596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-23</td>\n",
       "      <td>6716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>3754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>2853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>6618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>6505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-06</td>\n",
       "      <td>6747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-07</td>\n",
       "      <td>6609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-08</td>\n",
       "      <td>6700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-09</td>\n",
       "      <td>6615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-10</td>\n",
       "      <td>6696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-11</td>\n",
       "      <td>6673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-12</td>\n",
       "      <td>6637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>6508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-14</td>\n",
       "      <td>6600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-15</td>\n",
       "      <td>6549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-16</td>\n",
       "      <td>6545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>6538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-18</td>\n",
       "      <td>6603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-19</td>\n",
       "      <td>6552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-20</td>\n",
       "      <td>6679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-21</td>\n",
       "      <td>6560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-22</td>\n",
       "      <td>6669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-23</td>\n",
       "      <td>6633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>3681</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        group landing_page        date  user_id\n",
       "23    control     old_page  2017-01-02     2859\n",
       "24    control     old_page  2017-01-03     6590\n",
       "25    control     old_page  2017-01-04     6578\n",
       "26    control     old_page  2017-01-05     6427\n",
       "27    control     old_page  2017-01-06     6606\n",
       "28    control     old_page  2017-01-07     6604\n",
       "29    control     old_page  2017-01-08     6687\n",
       "30    control     old_page  2017-01-09     6628\n",
       "31    control     old_page  2017-01-10     6654\n",
       "32    control     old_page  2017-01-11     6688\n",
       "33    control     old_page  2017-01-12     6522\n",
       "34    control     old_page  2017-01-13     6552\n",
       "35    control     old_page  2017-01-14     6548\n",
       "36    control     old_page  2017-01-15     6714\n",
       "37    control     old_page  2017-01-16     6591\n",
       "38    control     old_page  2017-01-17     6617\n",
       "39    control     old_page  2017-01-18     6482\n",
       "40    control     old_page  2017-01-19     6578\n",
       "41    control     old_page  2017-01-20     6534\n",
       "42    control     old_page  2017-01-21     6749\n",
       "43    control     old_page  2017-01-22     6596\n",
       "44    control     old_page  2017-01-23     6716\n",
       "45    control     old_page  2017-01-24     3754\n",
       "46  treatment     new_page  2017-01-02     2853\n",
       "47  treatment     new_page  2017-01-03     6618\n",
       "48  treatment     new_page  2017-01-04     6541\n",
       "49  treatment     new_page  2017-01-05     6505\n",
       "50  treatment     new_page  2017-01-06     6747\n",
       "51  treatment     new_page  2017-01-07     6609\n",
       "52  treatment     new_page  2017-01-08     6700\n",
       "53  treatment     new_page  2017-01-09     6615\n",
       "54  treatment     new_page  2017-01-10     6696\n",
       "55  treatment     new_page  2017-01-11     6673\n",
       "56  treatment     new_page  2017-01-12     6637\n",
       "57  treatment     new_page  2017-01-13     6508\n",
       "58  treatment     new_page  2017-01-14     6600\n",
       "59  treatment     new_page  2017-01-15     6549\n",
       "60  treatment     new_page  2017-01-16     6545\n",
       "61  treatment     new_page  2017-01-17     6538\n",
       "62  treatment     new_page  2017-01-18     6603\n",
       "63  treatment     new_page  2017-01-19     6552\n",
       "64  treatment     new_page  2017-01-20     6679\n",
       "65  treatment     new_page  2017-01-21     6560\n",
       "66  treatment     new_page  2017-01-22     6669\n",
       "67  treatment     new_page  2017-01-23     6633\n",
       "68  treatment     new_page  2017-01-24     3681"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看我们的样本量是否满足最低样本量 \n",
    "dd=data.groupby([\"group\",\"landing_page\",\"date\"],as_index=False)[\"user_id\"].count()\n",
    "dd[((dd.group ==\"control\") & (dd.landing_page==\"old_page\"))|((dd.group ==\"treatment\") & (dd.landing_page==\"new_page\"))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "64021e69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group      landing_page\n",
       "control    new_page          1928\n",
       "           old_page        145274\n",
       "treatment  new_page        145311\n",
       "           old_page          1965\n",
       "Name: user_id, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查看样本量是否满足\n",
    "data.groupby([\"group\",\"landing_page\"])[\"user_id\"].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f358390",
   "metadata": {},
   "source": [
    "##### 利用统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3b1ff634",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>control</td>\n",
       "      <td>new_page</td>\n",
       "      <td>1.074479e+85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>inf</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>inf</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>treatment</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1.086957e+58</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       group landing_page     converted\n",
       "0    control     new_page  1.074479e+85\n",
       "1    control     old_page           inf\n",
       "2  treatment     new_page           inf\n",
       "3  treatment     old_page  1.086957e+58"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = data[data.date=='2017-01-03'].groupby([\"group\",\"landing_page\"],as_index=False)[\"converted\"].mean()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "74cedac5",
   "metadata": {},
   "outputs": [],
   "source": [
    "statistic_t = df.converted[2] - df.converted[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c99124b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6590 6618\n"
     ]
    }
   ],
   "source": [
    "# 用size求个数，为了求方差\n",
    "n1 = data[data.date=='2017-01-03'].converted[(data.group==\"control\") & (data.landing_page==\"old_page\")].size\n",
    "n2 = data[data.date=='2017-01-03'].converted[(data.group==\"treatment\") & (data.landing_page==\"new_page\")].size\n",
    "print(n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "74d283ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigma  = np.sqrt(df.converted[2] * (1-df.converted[2])/n2  + df.converted[1] * (1-df.converted[1])/n1 ).astype(float)\n",
    "sigma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1254f215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "statistic_p = 1-stats.norm.cdf(statistic_t,0,sigma)\n",
    "statistic_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "35186c46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "显著性P < α 实验组点击率 > 对照组\n"
     ]
    }
   ],
   "source": [
    "if(statistic_p > alpha):\n",
    "    print(\"显著性P > α 实验组点击率 <= 对照组\")\n",
    "else:\n",
    "    print(\"显著性P < α 实验组点击率 > 对照组\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "abe8cd24",
   "metadata": {},
   "outputs": [],
   "source": [
    "#封装如下\n",
    "def ABtest_P(df: pd.DataFrame, group_col: str = None, value_col: str = None ,alpha:float = 0.05):\n",
    "    '''\n",
    "    :param df: 被分析DateFrame对象\n",
    "    :param alpha: 显著性\n",
    "    :param group_col: 组列的名字，默认为df的第一列\n",
    "    :param value_col: 值列的名字,默认为df的第2列\n",
    "    :return: tongjiliang p_value p_type\n",
    "    '''\n",
    "        # 列名\n",
    "    if not group_col:\n",
    "        group_col = df.columns[0]\n",
    "    if not value_col:\n",
    "        value_col = df.columns[1]\n",
    "        \n",
    "    temp = df.groupby(group_col,as_index=False)[value_col].mean()\n",
    "    temp_n = df.groupby(group_col,as_index=False)[value_col].count()\n",
    "    tongjiliang =temp.iloc[0,1] - temp.iloc[1,1]\n",
    "        \n",
    "    diff_error =np.sqrt(temp.iloc[0,1] *(1-temp.iloc[0,1])/temp_n.iloc[0,1] +  temp.iloc[1,1] *(1-temp.iloc[1,1])/temp_n.iloc[1,1])\n",
    "    \n",
    "    tongjiliang_left_p = stats.norm.cdf(tongjiliang,0,diff_error)\n",
    "    tongjiliang_right_p = 1-stats.norm.cdf(tongjiliang,0,diff_error)\n",
    "    tongjiliang_site_p = tongjiliang_left_p *2;\n",
    "    if tongjiliang_site_p >1:\n",
    "        tongjiliang_site_p = tongjiliang_right_p*2\n",
    "    \n",
    "#     在封包的时候，我们左右双侧检验都用上，提高通用性\n",
    "    temp_l = [[temp.iloc[0,0],temp.iloc[1,0],tongjiliang,\"左侧\",tongjiliang_left_p,np.where(tongjiliang_left_p<alpha,\"显著\",\"不显著\")],\n",
    "              [temp.iloc[0,0],temp.iloc[1,0],tongjiliang,\"右侧\",tongjiliang_right_p,np.where(tongjiliang_right_p<alpha,\"显著\",\"不显著\")],\n",
    "              [temp.iloc[0,0],temp.iloc[1,0],tongjiliang,\"双侧\",tongjiliang_site_p,np.where(tongjiliang_site_p<alpha,\"显著\",\"不显著\")]]\n",
    "    temp = pd.DataFrame(temp_l,columns =[\"p\",\"p0\",\"统计量\",\"检测\",\"p_value\",\"结果\"])\n",
    "    \n",
    "    return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "719c2378",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>control</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>treatment</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>control</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        group converted\n",
       "38  treatment         0\n",
       "41  treatment         0\n",
       "57    control         0\n",
       "72  treatment         0\n",
       "84    control         0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp  = data[data.date=='2017-01-03'].loc[((data.group==\"control\") & (data.landing_page==\"old_page\")) \n",
    "                  | ((data.group==\"treatment\") & (data.landing_page==\"new_page\")),[\"group\",\"converted\"]]\n",
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "094fb614",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p</th>\n",
       "      <th>p0</th>\n",
       "      <th>统计量</th>\n",
       "      <th>检测</th>\n",
       "      <th>p_value</th>\n",
       "      <th>结果</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>左侧</td>\n",
       "      <td>NaN</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>右侧</td>\n",
       "      <td>NaN</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>control</td>\n",
       "      <td>treatment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>双侧</td>\n",
       "      <td>NaN</td>\n",
       "      <td>不显著</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p         p0  统计量  检测  p_value   结果\n",
       "0  control  treatment  NaN  左侧      NaN  不显著\n",
       "1  control  treatment  NaN  右侧      NaN  不显著\n",
       "2  control  treatment  NaN  双侧      NaN  不显著"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ABtest_P(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c6ccdfd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3.9",
   "language": "python",
   "name": "python3.9"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
